$(function(){
	const saveImgBtn = $('#saveImgBtn'),
		uploading = $('#uploading'),
        input_file = $('#input_file'),
        submitBtn = $('#submitBtn'),
        copyBtn = $('#copyBtn');

    const host = 'http://video.shnongc.cn';
    let imgUrl = '';

	function GetQueryString (name) { //获取指定name的url参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return (r[2]);
        return null;
    }
    function Toast (txt){
        var toastHtml = '<div class="tk_info" id="tkInfo"></div>';
        var toast = $('body #tkInfo');
        if (toast.length) {
            toast.html(txt).show();
        }else{
            $('body').append(toastHtml);
            $('body #tkInfo').html(txt).show();
        }
        setTimeout(function(){
          $('body #tkInfo').hide();
        },2000)
    }


    const userId = GetQueryString('userId')||'9381331';
    const token = GetQueryString('token')||'8c6429d4292647a8b7d7d3c457bb4647';
    const utId = GetQueryString('utId')||'123';


    //复制
    var btn=document.getElementById('copyBtn');
    var clipboard = new ClipboardJS('#copyBtn');
    clipboard.on('success', function(e){
        $('#show').slideDown().delay(1500).slideUp(300);
        console.log(e);
    });
    clipboard.on('error', function(e){
        $('#show').slideDown().delay(1500).slideUp(300);
        console.log(e);
    });

    //上传图片
    input_file.on('change',function(event){
        if(window.FileReader) {
            var reader = new FileReader();
        } else {
            alert("您的设备不支持图片预览功能，如需该功能请升级您的设备！");
            return false;
        }
        //获取文件
        var file = event.target.files[0];
        var imageType = /^image\//;
        //是否是图片
        if(!imageType.test(file.type)) {
            alert("请选择图片！");
            return;
        }
        reader.readAsDataURL(file);
        //读取完成
        reader.onloadend = function(e) {
            //图片路径设置为读取的图片
            let formDatas = new FormData();
            formDatas.append('file', $('#input_file')[0].files[0]);
            var data = e.target.result; 
            var img = uploading.find('img');
            img.attr('src',data)
            //ajax请求
            $.ajax({
                type: "post",
                url: host+"/system/api/appuser/uploadImage",
                data: formDatas,
                dataType: 'json',
                processData: false, // 告诉jQuery不要去处理发送的数据
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                xhrFields:{withCredentials:true},
                async: true,    //默认是true：异步，false：同步。
                success: function (d) {
                    let code = d.code;
                    let desc = d.msg||'网络错误';
                    if (code === 0 ) {
                        Toast('上传成功');
                        imgUrl =  d.data
                    }else{
                        Toast(desc);
                    }
                },
                error: function (data) {
                    Toast('请求异常');
                },
            })
                
        };
    })

    //提交审核
    submitBtn.on('click',function(){
        if (!imgUrl) {
            Toast('请选择图片！');
            return;
        }
         $.ajax({
            type: "get",
            url: host+"/system/api/appuser/submitTaskCheck",
            data: {
                'userId':userId,
                'token':token,
                'utId':utId,
                'image':imgUrl,
            },
            success: function (d) {
                let code = d.code;
                let desc = d.msg||'网络错误';
                if (code === 0 ) {
                    Toast(desc);
                    setTimeout(function(){
                        location.reload()
                    },2500)
                }else{
                    Toast(desc);
                }
            },
            error: function (data) {
                Toast('请求异常');
            },
        })
    })
})